<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.util.List" %>
<%@page import="java.util.ArrayList"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.io.InputStream" %>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Enroll Time Table</title>
	<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
	<script type="text/javascript" src="js/bootbox.js"></script>
	<link rel="stylesheet" type="text/css" href="css/stylev2.css" />
	<link rel="stylesheet" type="text/css" href="css/student_system.css" />
	
	<style type="text/css">
		a {
	      text-decoration:none;
	   }
	</style>
	
<script>
$(document).ready(function(){
	
	$("#frm_add_file").hide();
	$("#import_excel").click(function(){
		 
					$("#frm_add_file").toggle();
		 
	 });
	
});
function isValidUploadForm(oForm){
	
	//alert("Active isValidUploadForm");
	
	var _validFileExtensions = [".xlsx", ".xls"];
	  var arrInputs = oForm.getElementsByTagName("input");
	 
	  
	//  alert("arrInputs:"+ oForm.getElementsByTagName("input")[0].value);
	  
	  var file_name =  oForm.getElementsByTagName("input")[0].value
	  	if(file_name=="")
	  	{
	  		alert("กรุณาเลือกไฟล์ที่จะทำการ Import ในรูปแบบ xls, xlsx");
	  		return false;
	  	}
	  
	    for (var i = 0; i < arrInputs.length; i++) {
	        var oInput = arrInputs[i];
	        if (oInput.type == "file") {
	            var sFileName = oInput.value;
	            if (sFileName.length > 0) {
	                var blnValid = false;
	                for (var j = 0; j < _validFileExtensions.length; j++) {
	                    var sCurExtension = _validFileExtensions[j];
	                    if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
	                        blnValid = true;
	                        break;
	                    }
	                }

	                if (!blnValid) {
	                    alert("เกิดข้อผิดพลาด " + sFileName + " ไฟล์ที่แนบไม่ตรงรูปแบบของ : " + _validFileExtensions.join(", "));
	                    return false;
	                }
	            }
	        }
	    }

	    return true;
		
}
	
</script>
	
	<style type="text/css">
		td:nth-child(odd) {
			font-weight: bold;
		}
		td {
			height: 50px;
		}
	</style>
<style type="text/css">

.silvergrad:hover {
 
    cursor: pointer;
   
}


</style>
</head>
<body>

<%
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
	
	if (htp_session == null || htp_session.getAttribute("sUser") == null) {
	    // Forward the control to login.jsp if authentication fails or session expires
	    request.getRequestDispatcher("/login.jsp").forward(request,
	        response);
	}
	
	if("admin".equals(htp_session.getAttribute("sUser"))){%>
	<script>alert("This user dont have PERMISSION to access this zone.");</script>
	<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=admin_home.jsp"> 	
	<%}
	
	//////////get permission ///////////
	String name = String.valueOf(session.getAttribute("name"));
	String department_id = String.valueOf(session.getAttribute("department_id"));
	
	
	System.out.println("name : " + name);///////////////////////////////
	System.out.println("department_id : " + department_id);/////////////////////////
	
	
	Permission permission = new Permission(strUser,name);
	permission.setDepartmentId(department_id);
	permission.setRegisCreate(Boolean.valueOf(String.valueOf(session.getAttribute("enrollment_create"))));
	permission.setRegisDelete(Boolean.valueOf(String.valueOf(session.getAttribute("enrollment_delete"))));
	permission.setRegisRead(Boolean.valueOf(String.valueOf(session.getAttribute("enrollment_read"))));
	permission.setRegisUpdate(Boolean.valueOf(String.valueOf(session.getAttribute("enrollment_update"))));
	
	//check permission
		if(!permission.getRegisCreate()){%>
			<script>alert("This user dont have PERMISSION to access this zone.");</script>
			<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=enroll_home.jsp"> 	
		<%}
	
%>

	<div id="page-wrap">
		<div id="inside">
			<table border="0">
				<tr style="font-size:24px">
					<td width="600" height="100" style= "background-color: #273b80;border: 0px ;padding: 0px; 0px;" >
						<div id="header_banner-2"></div>
					</td>
					
					<td width="680" height="100" style= "background-color: #273b80;border: 0px;padding: 0px; 0px;" >
						<p align="right"><label><font size="4" color="#ffffff">username : <%=strUser%>&nbsp;&nbsp;</font></label></p>
						<a href="logout.jsp"><p align="right"><label style="cursor: pointer;"><font size="3" color="#ffffff">log out &nbsp;</font></label></p></a>
					</td>
				</tr>
			</table>
			<table border="0">
				<tr style="font-size:10px">
					<td width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="enroll_home.jsp"><h2><font color="#273b80"><label style="cursor: pointer;">หน้าแรก</label></font></h2></a></center>
					</td>
<%
			if(permission.getRegisRead()) {
%>
					<td width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="enroll_search.jsp"><h2><font color="#273b80"><label style="cursor: pointer;">ค้นหา</label></font></h2></a></center>
					</td>
					<td width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="enroll_summary.jsp"><h2><font color="#273b80"><label style="cursor: pointer;">สรุปผลการลงทะเบียน</label></font></h2></a></center>
					</td>
<%
			}
			if(permission.getRegisCreate()) {
%>
					<td width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="enroll_enrollment.jsp"><h2><font color="#273b80"><label style="cursor: pointer;">ลงทะเบียนรายวิชา</label></font></h2></center>
					</td>
					<td width = "720" height="50" style= "background-color:#FFFFFF ; border-bottom-color: #FFFFFF; border-left-color: #FFFFFF; border-right-color: #FFFFFF">
						<center><a href=""><h2><font color="#273b80"><label>ตารางสอน</label></font></h2></a></center>
					</td>
<%
			}
			
%>
				</tr>
			</table>
				
				<div id="main-content-ac">
				
				<center>	
					<table style="border: 0px;">
						<tr style="border: 0px;">
								<td style="border: 0px;">
									
								</td>
								<td style="border: 0px;" class="silvergrad" id="import_excel">
									<br>
									<br> <br>
									<button name="add_manual" id="btn_add_manual" style= "height: 40px; width: 140px; font-size:medium;">เพิ่มตารางสอน</button>
						
								</td>
							</tr>
					</table>
				</center>			
				
				<br> <br><br> <br>
						<div id="frm_add_file">
								<center>
											<table>
												<tr>
													<td>
														<center>
															 <form method="post" onsubmit="return isValidUploadForm(this);	" action="UploadServletTimeTable"enctype="multipart/form-data">
									        		  			 เลือกไฟล์ <input id="file_box" type="file" name="uploadFile"> 
									        		  			 <br>
									        		  			 <br>
									        		  			 <br>
									        		  			 
									        		  			 <input type="submit" value="Upload" />
									        		  			 
									        		  			 <br>
									        		  			 
									        		  			 </form>
									        		  			 
									        		  	</center>
									        		 </td>
												</tr>
													
											</table>
									</center>		
				
							</div>
					

								
						<br><br><br><br><br><br><br><br><br><br><br><br><br>
				
			<div style="clear: both;"></div>
			
			
				
			<div id="footer"></div>
			
		</div>		
			<div style="clear: both;"></div>		
   </div>
   
</body>
</html>